package org.example.realm;

import org.apache.ibatis.session.SqlSession;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.example.domain.Teacher;
import org.example.mapper.TeacherMapper;
import org.example.util.MyBatisUtil;

/**
 * @author 李新雨的项目86176
 * @version 1.0
 * @date 2024-12-15 19:32
 */
public class TeacherRealm extends AuthorizingRealm {
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        Object primaryPrincipal = principals.getPrimaryPrincipal();
        Teacher teacher= (Teacher) primaryPrincipal;
        System.out.println(teacher);
        return null;


    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String userName = (String) token.getPrincipal();
        //根据用户名查询数据库
        SqlSession session = MyBatisUtil.getSession();
        TeacherMapper mapper = session.getMapper(TeacherMapper.class);
        Teacher teacher= mapper.selectByName(userName);

        System.out.println("密码"+teacher.getPassword());
        System.out.println("盐值"+teacher.getSalt());
        ByteSource bytes = ByteSource.Util.bytes(teacher.getSalt());
        byte[] bytes1 = bytes.getBytes();
        String s = bytes1.toString();
        System.out.println(s);
        if (teacher ==null) {
            return null;
        }else {
            //
            return new SimpleAuthenticationInfo(teacher,teacher.getPassword(), ByteSource.Util.bytes(teacher.getSalt()),this.getName());
        }


    }
}
